package com.chendx.search.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chendx.model.behavior.dtos.BehaviorEntryDto;
import com.chendx.model.behavior.pojos.ApBehaviorEntry;
import com.chendx.model.common.dtos.ResponseResult;
import com.chendx.model.common.enums.AppHttpCodeEnum;
import com.chendx.model.search.dtos.UserSearchDto;
import com.chendx.model.search.pojos.ApUserSearch;
import com.chendx.model.user.pojos.ApUser;
import com.chendx.search.feign.BehaviorFeign;
import com.chendx.search.mapper.ApUserSearchMapper;
import com.chendx.search.service.ApUserSearchService;
import com.chendx.utils.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by chendx on 2022/3/16.
 */
@Service
public class ApUserSearchServiceImpl extends ServiceImpl<ApUserSearchMapper, ApUserSearch> implements ApUserSearchService {

    @Autowired
    private BehaviorFeign behaviorFeign;

    @Override
    public ResponseResult findUserSearch(UserSearchDto userSearchDto) {
        //1.检查数据
        if (userSearchDto.getPageSize() > 50) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.查询行为实体
        ApBehaviorEntry apBehaviorEntry = getEntry(userSearchDto);
        if (apBehaviorEntry == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        System.out.println(userSearchDto);
        IPage pageParam = new Page<>(userSearchDto.getPageNum(), userSearchDto.getPageSize());
        System.out.println(apBehaviorEntry +"  "+apBehaviorEntry.getEntryId());
        IPage<ApUserSearch> page
                = page(pageParam, Wrappers.<ApUserSearch>lambdaQuery().eq(ApUserSearch::getEntryId, apBehaviorEntry.getEntryId()).eq(ApUserSearch::getStatus, 1));

        return ResponseResult.okResult(page.getRecords());
    }

    @Override
    public ResponseResult delUserSearch(UserSearchDto userSearchDto) {
        //1.检查参数
        if (userSearchDto.getEquipmentId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApBehaviorEntry entry = getEntry(userSearchDto);
        if (entry == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.更新当前搜索记录的状态  status  0
        System.out.println(userSearchDto);
        update(Wrappers.<ApUserSearch>lambdaUpdate().eq(ApUserSearch::getId,userSearchDto.getId()).eq(ApUserSearch::getEntryId,entry.getEntryId())
                .set(ApUserSearch::getStatus,0));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Async("taskExecutor")
    @Override
    public void insert(Integer entryId, String searchWords) {

        ApUserSearch apUserSearch = getOne(Wrappers.<ApUserSearch>lambdaQuery().eq(ApUserSearch::getEntryId, entryId).eq(ApUserSearch::getKeyword, searchWords));
        if (apUserSearch != null && apUserSearch.getStatus() == 1) {
            return;
        } else if (apUserSearch != null && apUserSearch.getStatus() == 0) {
            apUserSearch.setStatus(1);
            updateById(apUserSearch);
            return;
        }
        apUserSearch = new ApUserSearch();
        apUserSearch.setEntryId(entryId);
        apUserSearch.setStatus(1);
        apUserSearch.setKeyword(searchWords);
        apUserSearch.setCreatedTime(new Date());
        save(apUserSearch);
    }

    private ApBehaviorEntry getEntry(UserSearchDto userSearchDto) {
        BehaviorEntryDto dto = new BehaviorEntryDto();
        ApUser apUser = AppThreadLocalUtils.get();
        if (apUser != null && apUser.getId() != null) {
            dto.setUserId(apUser.getId());
        }
        dto.setEquipmentId(userSearchDto.getEquipmentId());
        ApBehaviorEntry apBehaviorEntry = behaviorFeign.findByUserIdOrEquipmentId(dto);
        return apBehaviorEntry;
    }
}
